home *** CD-ROM | disk | FTP | other *** search
/ Risc World 3 / Risc World 3.iso / SOFTWARE / ISSUE4 / POWERBASE / DOCS / Ch08-CSVs < prev    next >
Text File  |  2001-01-29  |  18KB  |  326 lines

  1. ======================
  2. Ch 8 − Using CSV files
  3. ======================
  4.  
  5. 8.1     What are CSV files?
  6. ---------------------------
  7. CSV stands for “comma-separated values” and is the name given to files
  8. consisting of lines of data, each of which contains individual data items
  9. separated from each other by commas. Such files are widely used to
  10. mail-merge using a wordprocessor (see Ch 9) and also to transfer data from
  11. one application to another, e.g. from a database to a spreadsheet or from a
  12. RISC OS database to a PC database. We will refer to each line in a CSV file
  13. as a record and each item of data in such a line as a field since records
  14. and fields are the source and destination of such data when it is exported
  15. from or imported into a Powerbase database. The following points should be
  16. noted:
  17.  
  18. (1) Fields which are non-numeric (e.g. plain text items such as names and
  19. addresses) are often enclosed in double quotes (“”) whereas numeric data is
  20. not. This makes it possible for an application reading a CSV file to
  21. distinguish between numbers and strings (which might of course contain
  22. numerals) and also allows a comma to be used as a character within a string
  23. without being mistaken for a data separator. (Addresses often contain commas
  24. e.g. 112, Keighley Road).  For many purposes the quotes aren’t necessary and
  25. may be omitted.
  26.  
  27. (2) Null fields are usually included and can be located by looking for two
  28. commas with either nothing in between them or with only two double quotation
  29. marks between them, i.e. ,, or ,“”,. Each record in such a file will always
  30. contain the same number of fields. Powerbase does, however, allow nulls to
  31. be omitted entirely when creating a file in which case the number of fields
  32. per record will vary.
  33.  
  34. (3) The way in which each record is terminated varies from one system to
  35. another. The last field in a record is followed not by a comma but by a line
  36. terminator. On RISC OS systems this is the same as in ordinary text files:
  37. the linefeed character (LF, ASCII value 10) and on PCs it is usually the
  38. carriage-return character (CR, ASCII value 13). You might, however,
  39. encounter CSV files in which both these characters are used, i.e. LF CR or
  40. CR LF. Powerbase lets you define any character or pair of characters as the
  41. record terminator.
  42.  
  43. (4) Separators other than commas are sometimes used. The Tab character
  44. (ASCII value 9) is often used and such files are called TSV (“tab-separated
  45. values”) files. Powerbase lets you define any character, or even a pair of
  46. characters, as the field separator. All such files created by Powerbase will
  47. be of type &dfe and display the CSV file icon (although the default
  48. filenames offered do differ: “CSVfile” where the separator really is a
  49. comma, “TSVfile” where it’s a TAB and “?SVfile” otherwise). In what follows
  50. we will, for convenience, refer to them all as “CSV files” whether or not
  51. the separator is a comma.
  52.  
  53. (5) Some applications which accept a CSV file as input expect the first line
  54. to contain the names of the fields which comprise the subsequent records,
  55. e.g. if each record consists of a name and a four-part address this header
  56. record might read: 
  57.  
  58.         “NAME”,“STREET”,“TOWN”,“COUNTY”,“POSTCODE”
  59.  
  60. Powerbase can both export and import files with such a header.
  61.  
  62. 8.2     Setting the CSV options
  63. -------------------------------
  64. Choosing CSV files => Options from the main menu displays the CSV options
  65. window which lets you specify all the file characteristics described
  66. earlier.  Pop-up menus give you a choice of field-separators and
  67. record-terminators with space to enter your own if you wish.  The first
  68. three option switches cause an exported file to have, respectively, the
  69. following characteristics when the switch is selected:
  70.  
  71. •       quotes round non-numeric fields − see (1) above
  72.  
  73. •       a header record specifying the field names as either tags or
  74.         descriptors − see (5) above
  75.  
  76. •       null fields included − see (2) above
  77.  
  78. Note that the field names referred to in a header record are, by default,
  79. the tags of the corresponding Powerbase fields, but may be changed to the
  80. descriptors by altering the setting in the Print options window.
  81.  
  82.                                 - 44 - 
  83.  
  84. 8.3     Exporting data as a CSV file
  85. ------------------------------------
  86. Having set up your options as described above, creating a CSV file is very
  87. like printing a list. First highlight the fields to be exported by clicking
  88. on each with ADJUST. Remember that the order in which the fields are
  89. highlighted is important. Next choose CSV files =>Export (Ctrl X). A window
  90. featuring the Query panel appears. Type in a search formula to determine
  91. which records are exported. Finally, enter the name of the file and drag the
  92. file icon to a filer window. By default the file is saved in PrintJobs as
  93. usual, and you may simply click on the Export button or type Return.
  94. Experiment with saving CSV files with different settings of the CSV options
  95. and then loading the resulting files into Edit to examine them.  If you
  96. select the Reverse switch on the Query panel the CSV file will be created in
  97. reverse order. (See also 3.5.1).
  98.  
  99. The field-concatenation option (see 3.2.2) applies.  This means that data
  100. which occupies separate fields in the Powerbase record need not do so in the
  101. exported CSV file.  By holding down Shift when selecting the field with
  102. ADJUST, the comma (or other separator) which would normally follow is
  103. suppressed until you select a field without using Shift.  A slightly
  104. problematic situation occurs when you concatenate a mixture of numeric and
  105. non-numeric fields with the In quotes option selected.  In such a case
  106. Powerbase will enclose the whole concatenated group within two sets of
  107. double quotes. e.g. NAME, Z, M and SYM from the Elements database would be
  108. exported, for actinium, as:
  109.  
  110.         “ACTINIUM 89 227 Ac”
  111.  
  112. The Spacer (see 3.10) − in the above instance it is the default setting of
  113. one space − is used to separate the concatenated fields.
  114.  
  115. 8.3.1   Scrollable lists and CSV files
  116.  
  117. Scrollable fields may appear in a CSV file in two different ways, depending
  118. on how their printing option is set in the Print Options window. If As
  119. single row is selected the whole list is exported as a single CSV field. The
  120. data corresponding to individual rows of the list are separated by
  121. semicolons. If As columns is selected then each row of the list is exported
  122. as a CSV field.
  123.  
  124. Transferring data from and to scrollable lists in individual records was
  125. covered in 2.6.5 and 2.6.6.
  126.  
  127. 8.4     Using CSV files to import data
  128. --------------------------------------
  129. If you drop a CSV file on the record window of an open database the CSV
  130. options window (see 8.2) appears with the title changed to “Import CSV file”
  131. and some additional icons, one of which displays the pathname of the file. 
  132. Clicking on Import will make Powerbase try to create new database records
  133. from the file. If you decide not to do this you should click on Cancel. If
  134. you do wish to import the data there are some important consideration which
  135. will now be explained.
  136.  
  137. 8.4.1   Ensuring that the correct options are selected
  138.  
  139. If the CSV file originally came from a Powerbase application the settings in
  140. the CSV options window need to be exactly the same as they were when the
  141. file was exported. The exception to this is the In quotes button which is
  142. shaded on import because Powerbase doesn’t need it. If the CSV file came
  143. from a PC or another RISC OS application you might have to load it into Edit
  144. to find out what separator and terminator are used.  You will also probably
  145. need to set the filetype to &dfe (omit the “&”) as well.
  146.  
  147. There are three more option switches at the bottom of the window. One causes
  148. each record to be displayed as it is imported. The import process is slower
  149. with this turned on but much more informative if you like to know how things
  150. are progressing. The second switch, when selected, strips any trailing
  151. spaces in the imported data-fields. Some database programs pad all fields to
  152. their maximum length by adding spaces to the end of the data where
  153. necessary.  If you import such a file into Powerbase you will find that the
  154. caret will always be at the far right of the field even though visible
  155. characters do not fill the field, and some queries won’t work properly.
  156. Setting the Strip spaces switch before importing the file overcomes the
  157. problem.  The third option switch determines how Sequence number fields are
  158. handled.  If the switch is ON imported sequence numbers are ignored and new
  159. ones assigned in accordance with the field’s sequence number counter.  With
  160. the switch OFF sequence numbers from the CSV file are imported without
  161. alteration.
  162.  
  163. 8.4.2   Directing imported data to the correct fields
  164.  
  165. If no fields on the record window are highlighted (i.e. with ADJUST) and the
  166. CSV file does not contain a header record (see 8.1) then the import process
  167. proceeds according to the following rules:
  168.  
  169.                                 - 45 -
  170.  
  171. •       The first CSV field will be read into the first Powerbase field for
  172.         which importing is allowed, This means any which can hold text
  173.         (including External fields) or numbers and also check-boxes Graphics
  174.         fields, Buttons or fields which are merely labels will be ignored.
  175.         The next CSV field will be read into the second Powerbase field and
  176.         so on. 
  177.  
  178. •       If the end of the CSV record is reached before all the relevant
  179.         fields have been filled (data underflow) then the next CSV record
  180.         will start a new Powerbase record, i.e. the reading won’t get out of
  181.         step. It does not matter, therefore, if the CSV file omits null
  182.         items at the end of a line. 
  183.  
  184. •       If all relevant fields are filled before reaching the end of the CSV
  185.         record (data overflow) Powerbase ignores the remainder of the line
  186.         and skips to the beginning of the next CSV record before starting a
  187.         new Powerbase record. This is also to keep the operation in step. 
  188.  
  189. There will be occasions when you don’t want to fill the Powerbase fields
  190. sequentially as just described. There are two ways of making the process
  191. more specific: 
  192.  
  193. (1) Highlight the required Powerbase fields with ADJUST before starting the
  194. import.  Data will then be read only into the highlighted fields, all other
  195. fields being ignored. The order in which the fields are filled is the order
  196. in which you highlighted them.  The rules given above about underflow and
  197. overflow of data still apply.
  198.  
  199. (2) Give the file a header record containing the tags or descriptors of
  200. required fields in the Powerbase record.  There is nothing to prevent you
  201. using Edit  to add such a header to a CSV file which did not originate from
  202. a Powerbase application. The format of the header is illustrated in 8.1 (5). 
  203. Importing then occurs just as if those fields were highlighted. The Print
  204. options window must reflect whether the tags or descriptors of fields are
  205. used. Do not use both a header and highlighting.
  206.  
  207. 8.4.3   Importing data from plain text files
  208.  
  209. It was explained in 8.1 (4) that files created as described above (8.3) can
  210. have separators other than a comma. If the file has been created using
  211. Powerbase’s CSV exporting facility it will have been given the file-type
  212. &dfe as if it was a true CSV file. If it comes from some other source it is
  213. likely to be of type &fff (Text). It can still be imported but caution is
  214. needed because there are other circumstances in which a text file might be
  215. dropped on the record window. A properly-written script file (see Ch 12)
  216. would be recognised as such and therefore cause no problem, but any text
  217. file dropped onto the appropriate type of External field (i.e. a Text or
  218. Text Block field) would become linked to that field instead of being treated
  219. like a CSV file. If you are importing data from a plain text file and your
  220. record contains fields of the aforementioned types be sure to drop the file
  221. on the window background, not on the External field The appropriate window
  222. will then appear with the title “Import text file”. 
  223.  
  224. You are strongly advised to use proper CSV files if at all possible.  You
  225. could, in fact, use the filer to set the filetype of such non-standard files
  226. to &dfe (omit the “&”) so that the problem of Powerbase taking the wrong
  227. action doesn’t arise.  Be sure to use Options to set the correct field
  228. separator and record terminator though. 
  229.  
  230. 8.4.4   What if the imported data won’t fit?
  231.  
  232. There are two situations in which this can happen. The database might not
  233. contain enough free records to hold all the imported data and so you get a
  234. “Database full when reading CSV file” error. To avoid this either make sure
  235. the database is big enough before you start or place a suitable value in the
  236. Increment for expansion icon in the Change length window.  The latter is
  237. accessible from the Utilities submenu of the iconbar menu (see 10.5).
  238.  
  239. The second situation is where an item is too long for the destined database
  240. field. When importing data Powerbase maintains a file called TooBig inside
  241. the database’s PrintJobs directory. Anything which won’t fit in the target
  242. field is instead written to this file together with information about where
  243. it was intended to go and an “@” character is placed in the database field
  244. to draw your attention to it. No writable Powerbase field may be longer than
  245. 246 characters and if an item of imported data exceeds this a note will be
  246. made in the TooBig file advising you to define an External field (Text Block
  247. or Text) for such data. After completing a CSV import operation it is
  248. advisable to look at the TooBig file to see if any remedial action is
  249. needed.
  250.  
  251.                                 - 46 -
  252.  
  253. 8.5     Using CSV files to modify existing records
  254. --------------------------------------------------
  255. Most database programs which support CSV import allow it to be used only for
  256. creating new records.  Powerbase is unusual in that you can use a CSV file
  257. to modify existing records. This capability should be used with caution
  258. since careless use can irrevocably garble a database.  There are three
  259. relevant radio buttons in the CSV options window. They are called Modify
  260. existing, With primary key and With rec. number and their actions are as
  261. follows:
  262.  
  263. Modify existing No new records will be created when a CSV file is dropped
  264. onto the record window.  The existing records will be accessed in the order
  265. determined by the current index and the new data will be merged into these
  266. records.  You should, of course, either use ADJUST to highlight the fields
  267. into which the data is to go or place a header in the CSV file specifying
  268. the field tags and then turn on the With header switch (see 8.4.2).  If all
  269. the records are modified before the end of the CSV file has been reached a
  270. warning message is displayed.  Note that it is the user’s responsibility to
  271. ensure that the data in the CSV file is in the correct order since Powerbase
  272. has no way of telling which data is destined for which record and can only
  273. proceed sequentially.  As an additional precaution a warning message is
  274. displayed when you choose this option.
  275.  
  276. With primary key This affects both export and import.  When you export data
  277. with this button selected  each record of the CSV file includes the primary
  278. key of the Powerbase record. (Try creating a CSV file with and without this
  279. option set and compare the files using Edit.)  When importing such a file
  280. Powerbase will attempt to locate records with the same primary keys as the
  281. records in the CSV file. If a matching key is not found a new record will be
  282. created to receive the data. If the key does exist then the CSV data goes
  283. into the same record, overwriting any data which the target fields already
  284. contain.  The option is only useful for transferring data between Powerbase
  285. databases which have the same primary key field(s) and structure.  Don’t
  286. forget that Powerbase, by default, allows duplicate primary keys so it is
  287. possible for the new data to go into the wrong record.  You would be
  288. ill-advised to trust this method of import unless you are sure each primary
  289. key is unique.
  290.  
  291. With record number This resembles the previous option. It allows data to be
  292. exported with the Powerbase record numbers included. Importing into another
  293. (or the same) database places the data in records having the same record
  294. numbers, again overwriting data which may already be present in the target
  295. fields.  This option is only useful for transferring data between Powerbase
  296. databases in which corresponding records have identical record numbers.  One
  297. use for it might be to export a set of data, load it into an editor and do
  298. some extensive searching-and-replacing, then put the modified data back into
  299. the original database. 
  300.  
  301. 8.6     Creating a new, working database from a CSV file
  302. --------------------------------------------------------
  303. There is an option button in the CSV options window called With field data
  304. which only becomes active when the With header button is selected. With the
  305. switch selected the header record of a saved CSV file contains not only the
  306. field names but also the field lengths and types. Again it is suggested that
  307. you create a file and look at it in Edit. Each item in the header begins
  308. with a number. This is the field length in characters, i.e. the maximum
  309. allowed length in the Powerbase record field. Next comes a ¤ character which
  310.  
  311. separates the length from the field name. Another ¤ separates the field name
  312. from the concluding number which determines the field type. (The type
  313. numbers may be inspected by looking at the ValStrings file in the Powerbase
  314. directory.) 
  315.  
  316. A file created in this form is not meant to be dropped onto the window of an
  317. open database. It should be dropped onto the Powerbase icon on the iconbar
  318. when no database is open. Powerbase will attempt to convert the file into a
  319. functioning database. All the fields will be ranged on the left of the
  320. record window, one beneath another and the primary key will consist of the
  321. first four characters of the first field. Don’t try to create databases
  322. containing Check box, Button or External fields using this method; it’s only
  323. meant for the most basic type of database in which all fields are of the
  324. Editable class.
  325.  
  326.                                 - 47 -